package top.milkbox.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import top.milkbox.info.R;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@Component
public class WorkerInterceptor extends BaseInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        R r = new R();
        int userRoleId = getUserRole(request.getCookies());
        r.setFlag(userRoleId == 2 || userRoleId == 3); // 判断用户是否是维修员或管理员
        if (!r.isFlag()) {
            log.debug("用户不是维修人员");
            r.setMessage("抱歉您不是维修人员，没有权限访问");
            response.setContentType("application/json;charset=utf-8");
            objectMapper.writeValue(response.getWriter(), r);
        }
        return r.isFlag();
    }
}
